/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */
package com.fuyao.cloud.space.tree.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fuyao.cloud.common.data.datascope.DataScope;
import com.fuyao.cloud.space.constant.EdificeConstant;
import com.fuyao.cloud.space.constant.HouseConstant;
import com.fuyao.cloud.space.constant.RegionalConstant;
import com.fuyao.cloud.space.constant.StoreyConstant;
import com.fuyao.cloud.space.edifice.mapper.RoomEdificeMapper;
import com.fuyao.cloud.space.edifice.service.RoomEdificeService;
import com.fuyao.cloud.space.entity.RoomEdifice;
import com.fuyao.cloud.space.entity.RoomHouse;
import com.fuyao.cloud.space.entity.RoomRegional;
import com.fuyao.cloud.space.entity.RoomStorey;
import com.fuyao.cloud.space.house.mapper.RoomHouseMapper;
import com.fuyao.cloud.space.regional.mapper.RoomRegionalMapper;
import com.fuyao.cloud.space.regional.service.RoomRegionalService;
import com.fuyao.cloud.space.storey.mapper.RoomStoreyMapper;
import com.fuyao.cloud.space.tree.service.SpaceTreeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Service
@RequiredArgsConstructor
public class SpaceTreeServiceImpl implements SpaceTreeService {

    private final RoomRegionalMapper roomRegionalMapper;
    private final RoomEdificeMapper roomEdificeMapper;
    private final RoomStoreyMapper roomStoreyMapper;
    private final RoomHouseMapper roomHouseMapper;

    @Override
    public List<Tree<String>> edificeTree() {
        List<TreeNode<String>> collect = new ArrayList<>();
        collect.addAll(getRegionalTreeList());
        collect.addAll(getEdificeTreeList());
        return TreeUtil.build(collect, RegionalConstant.REGIONAL_TREE_NAME + ".0");
    }

    @Override
    public List<Tree<String>> storeyTree() {
        List<TreeNode<String>> collect = new ArrayList<>();
        collect.addAll(getRegionalTreeList());
        collect.addAll(getEdificeTreeList());
        collect.addAll(getStoreyTreeList());
        return TreeUtil.build(collect, RegionalConstant.REGIONAL_TREE_NAME + ".0");
    }

    @Override
    public List<Tree<String>> houseTree() {
        List<TreeNode<String>> collect = new ArrayList<>();
        collect.addAll(getRegionalTreeList());
        collect.addAll(getEdificeTreeList());
        collect.addAll(getStoreyTreeList());
        collect.addAll(getHouseTreeList());
        return TreeUtil.build(collect, RegionalConstant.REGIONAL_TREE_NAME + ".0");
    }

    /**
     * 获取区域树集合
     * @return
     */
    private List<TreeNode<String>> getRegionalTreeList(){
        // 查询全部区域
        List<RoomRegional> regionalAllList = roomRegionalMapper.selectList(Wrappers.emptyWrapper());
        // 查询数据权限内部门
        List<Long> storeyOwnIdList = roomRegionalMapper.selectListByScope(Wrappers.emptyWrapper(), new DataScope()).stream()
                .map(RoomRegional::getDeptId).collect(Collectors.toList());
        return regionalAllList.stream()
                .sorted(Comparator.comparing(RoomRegional::getCreateTime)).map(regional -> {
                    TreeNode<String> treeNode = new TreeNode();
                    treeNode.setId(RegionalConstant.REGIONAL_TREE_NAME + "." + regional.getId());
                    treeNode.setParentId(RegionalConstant.REGIONAL_TREE_NAME + "." + regional.getParentId());
                    treeNode.setName(regional.getName());
                    treeNode.setWeight(regional.getOrdernumber());
                    // 有权限不返回标识
                    Map<String, Object> extra = new HashMap<>(8);
                    extra.put("isLock", !storeyOwnIdList.contains(regional.getDeptId()));
                    extra.put("createTime", regional.getCreateTime());
                    extra.put("affiliName", RegionalConstant.REGIONAL_TREE_NAME);
                    extra.put("affiliParentName", RegionalConstant.REGIONAL_TREE_NAME);
                    extra.put("dataCode", regional.getDataCode());
                    extra.put("idCard", regional.getId());
                    treeNode.setExtra(extra);
                    return treeNode;
                }).collect(Collectors.toList());
    }

    /**
     * 获取楼栋树集合
     * @return 楼栋树集合
     */
    private List<TreeNode<String>> getEdificeTreeList(){
        // 查询全部楼栋
        List<RoomEdifice> edificeAllList = roomEdificeMapper.selectList(Wrappers.emptyWrapper());
        // 查询数据权限内部门
        List<Long> storeyOwnIdList = roomEdificeMapper.selectListByScope(Wrappers.emptyWrapper(), new DataScope()).stream()
                .map(RoomEdifice::getDeptId).collect(Collectors.toList());
        return edificeAllList.stream()
                .sorted(Comparator.comparing(RoomEdifice::getCreateTime)).map(edifice -> {
                    TreeNode<String> treeNode = new TreeNode();
                    treeNode.setId(EdificeConstant.EDIFICE_TREE_NAME + "." + edifice.getId());
                    treeNode.setParentId(RegionalConstant.REGIONAL_TREE_NAME + "." + edifice.getRegionalId());
                    treeNode.setName(edifice.getName());
                    treeNode.setWeight(edifice.getOrdernumber());
                    // 有权限不返回标识
                    Map<String, Object> extra = new HashMap<>(8);
                    extra.put("isLock", !storeyOwnIdList.contains(edifice.getDeptId()));
                    extra.put("createTime", edifice.getCreateTime());
                    extra.put("affiliName", EdificeConstant.EDIFICE_TREE_NAME);
                    extra.put("affiliParentName", RegionalConstant.REGIONAL_TREE_NAME);
                    extra.put("idCard", edifice.getId());
                    extra.put("dataCode", edifice.getDataCode());
                    treeNode.setExtra(extra);
                    return treeNode;
                }).collect(Collectors.toList());
    }

    /**
     * 获取楼层树集合
     * @return 楼栋树集合
     */
    private List<TreeNode<String>> getStoreyTreeList(){
        // 查询全部楼栋
        List<RoomStorey> roomStoreyList = roomStoreyMapper.selectList(Wrappers.emptyWrapper());
        // 查询数据权限内部门
        List<Long> storeyOwnIdList = roomStoreyMapper.selectListByScope(Wrappers.emptyWrapper(), new DataScope()).stream()
                .map(RoomStorey::getDeptId).collect(Collectors.toList());
        return roomStoreyList.stream()
                .sorted(Comparator.comparing(RoomStorey::getCreateTime)).map(roomStorey -> {
                    TreeNode<String> treeNode = new TreeNode();
                    treeNode.setId(StoreyConstant.STOREY_TREE_NAME + "." + roomStorey.getId());
                    treeNode.setParentId(EdificeConstant.EDIFICE_TREE_NAME + "." + roomStorey.getEdificeId());
                    treeNode.setName(roomStorey.getName());
                    treeNode.setWeight(roomStorey.getOrdernumber());
                    // 有权限不返回标识
                    Map<String, Object> extra = new HashMap<>(8);
                    extra.put("isLock", !storeyOwnIdList.contains(roomStorey.getDeptId()));
                    extra.put("createTime", roomStorey.getCreateTime());
                    extra.put("affiliName", StoreyConstant.STOREY_TREE_NAME);
                    extra.put("affiliParentName", EdificeConstant.EDIFICE_TREE_NAME);
                    extra.put("idCard", roomStorey.getId());
                    extra.put("dataCode", roomStorey.getDataCode());
                    treeNode.setExtra(extra);
                    return treeNode;
                }).collect(Collectors.toList());
    }

    /**
     * 获取楼层树集合
     * @return 楼栋树集合
     */
    private List<TreeNode<String>> getHouseTreeList(){
        // 查询全部楼栋
        List<RoomHouse> roomStoreyList = roomHouseMapper.selectList(Wrappers.emptyWrapper());
        // 查询数据权限内部门
        List<Long> storeyOwnIdList = roomHouseMapper.selectListByScope(Wrappers.emptyWrapper(), new DataScope()).stream()
                .map(RoomHouse::getDeptId).collect(Collectors.toList());
        return roomStoreyList.stream()
                .sorted(Comparator.comparing(RoomHouse::getCreateTime)).map(house -> {
                    TreeNode<String> treeNode = new TreeNode();
                    treeNode.setId(HouseConstant.HOUSE_TREE_NAME + "." + house.getId());
                    treeNode.setParentId(StoreyConstant.STOREY_TREE_NAME + "." + house.getStoreyId());
                    treeNode.setName(house.getName());
                    treeNode.setWeight(house.getOrdernumber());
                    // 有权限不返回标识
                    Map<String, Object> extra = new HashMap<>(8);
                    extra.put("isLock", !storeyOwnIdList.contains(house.getDeptId()));
                    extra.put("createTime", house.getCreateTime());
                    extra.put("affiliName", HouseConstant.HOUSE_TREE_NAME);
                    extra.put("affiliParentName", StoreyConstant.STOREY_TREE_NAME);
                    extra.put("idCard", house.getId());
                    extra.put("dataCode", house.getDataCode());
                    treeNode.setExtra(extra);
                    return treeNode;
                }).collect(Collectors.toList());
    }

}
